package com.dotflat.application.controller;

import com.dotflat.application.domain.AppForm;
import com.dotflat.application.po.AppFormPO;
import com.dotflat.application.po.LeadsPO;
import com.dotflat.application.service.AppFormService;
import com.dotflat.rest.controller.RestfulBaseController;
import com.starinside.wp.domain.WxUser;
import com.starinside.wp.foundation.dto.AreaDTO;
import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.foundation.query.base.PagerQuery;
import com.starinside.wp.utils.RemoteCallUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.starinside.wp.foundation.constant.RequestConstant.TICKET;


@RestController
@RequestMapping("/v2/op/appform")
@Slf4j
@Api(tags = "运营 - 入园入托申请表")
public class AppFormController extends RestfulBaseController<AppForm, AppFormService, AppFormPO> {

    @Autowired
    public void setBaseService(AppFormService service){
        super.setBaseService(service);
    }

    @Value("${sso.server.url}")
    private String ssoServerUrl;

    @ApiOperation(value = "获取最近的五十个幼儿园")
    @PostMapping("/local_areas")
    @Transactional(rollbackFor = RuntimeException.class)
    public StatusResponse localAreas(String latitude, String longtitude){

        Map map = new HashMap();
        map.put("lat",latitude);
        map.put("lon",longtitude);
        map.put("num",20);

        List result =  RemoteCallUtil.callOfList(ssoServerUrl + "/api/sso/other_system/areasByLocation", map, null, HashMap.class, "获取附近园区失败");

        return StatusResponse.success(result);
    }

    @ApiOperation(value = "根据city名字模糊匹配幼儿园地址")
    @PostMapping("/local_city_areas")
    @Transactional(rollbackFor = RuntimeException.class)
    public StatusResponse localAreas(String cityName){

        Map map = new HashMap();
        map.put("pcaName",cityName);
        List<AreaDTO> result =   RemoteCallUtil.callOfList(ssoServerUrl + "/api/sso/other_system/areasByLocationLike", map, null, AreaDTO.class, "调用sso推送消息出错");

        return StatusResponse.success(result);
    }

    @ApiOperation(value = "增加一条记录", response = AppForm.class)
    @PostMapping()
    @Transactional(rollbackFor = RuntimeException.class)
    public StatusResponse add(@RequestHeader(value = TICKET, required = false) String ticket,
                              HttpServletRequest req,
                              @Valid AppFormPO param){
        WxUser user = (WxUser) req.getSession().getAttribute("loginUser");
         //创建
        AppForm ret = baseService.add(param.getAreaId(), "0", param);

        //创建一条线索
        LeadsPO leads = baseService.addLeadsByAppForm(ret, user);
        log.info("leads added :::\n" + leads);

        return StatusResponse.success(ret);
    }

    @Override
    @ApiOperation(value = "获取列表", hidden = true)
    @GetMapping("/list")
    public StatusResponse getList(@RequestHeader(TICKET) String ticket, HttpServletRequest req, AppFormPO param, PagerQuery pagerQuery){
        return StatusResponse.success();
    }

    @Override
    @ApiOperation(value = "获取一个对象", hidden = true)
    @GetMapping()
    public StatusResponse get(@RequestHeader(TICKET) String ticket, HttpServletRequest req, String id){
        return StatusResponse.success();
    }

}
